package com.starwish.starwishblog.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starwish.starwishblog.entity.Manager;
import com.starwish.starwishblog.mapper.ManagerMapper;

import com.starwish.starwishblog.vo.ManagerLoginVo;
import com.starwish.starwishblog.vo.ManagerOperationVo;
import com.starwish.starwishblog.vo.ManagerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ManagerService {
    @Autowired
    ManagerMapper managerMapper;

    public Manager login(String name){
        return managerMapper.selectManagerByName(name);
    }

    public PageInfo<ManagerVo> queryManagerVoByCons(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<ManagerVo> list = managerMapper.selectManagerVoByCons(name);
//        for (ManagerVo managerVo : list) {
//            System.out.println(managerVo);
//        }
        return new PageInfo<>(list);
    }


    public Manager queryManagerByKey(Integer id) {
        return managerMapper.selectByPrimaryKey(id.intValue());
    }

    @Transactional
    public int modify(ManagerVo managerVo) {
        //清空用户的所有角色
        int i = managerMapper.deleteRolesByMid(managerVo.getId());
        //新增用户的所有角色
        int j = managerMapper.insertRolesToManager(managerVo.getId(),managerVo.getRoleIds());
        //修改用户的其他信息

        return managerMapper.update(managerVo);
    }

    @Transactional
    public int addManager(ManagerVo managerVo) {
        //先插入基本信息
        int i = managerMapper.insertManager(managerVo);
        System.out.println(managerVo.getId());
        int j = managerMapper.insertRolesToManager(managerVo.getId(),managerVo.getRoleIds());
        return i;
    }

    @Transactional
    public int removeByKey(Integer id) {
        managerMapper.deleteByPrimaryKey(id);
        return managerMapper.deleteRolesByMid(id);
    }

    public int updateLocked(Long id,Integer status) {
        return managerMapper.updateLocked(id,status);
    }

    public int addManagerLogin(ManagerLoginVo managerLoginVo) {
        int i = managerMapper.insertManagerLogin(managerLoginVo);
        return i;
    }

    public int addManagerOperation(ManagerOperationVo operationVo) {
        int i = managerMapper.insertManagerOperation(operationVo);
        return i;
    }
}
